package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;

import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.model.RMain;

/**
 * Created by AAA on 2017/4/21.
 */

public class YxdMajorMainAdpter extends BaseAdapter{
    private Context context;
    private ArrayList<RMain.TablesBean.Q1Bean> list;
    private BtnClick btnClick;
    private int defItem=-1;
    private List<RMain.TablesBean.Q1Bean> checkList;
    public YxdMajorMainAdpter(Context context, ArrayList<RMain.TablesBean.Q1Bean> list,List<RMain.TablesBean.Q1Bean> checkList, BtnClick btnClick){
        this.context=context;
        this.list=list;
        this.checkList = checkList;
        this.btnClick=btnClick;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    public void setDefItem(int position){
        this.defItem=position;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.yxd_item_info,parent,false);
            holder=new ViewHolder();
            holder.majorItem= (RadioButton) convertView.findViewById(R.id.majorItem);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        if(list.get(position)!=null){
            holder.majorItem.setText(list.get(position).getReportTableName());
            if (checkList!=null&&checkList.size()>0){
                int count = 0;
                for (RMain.TablesBean.Q1Bean q1:checkList){
                    if (q1.getID() == list.get(position).getID()){
                        count++;
                        holder.majorItem.setBackgroundColor(context.getResources().getColor(R.color.pink_light));
                    }
                }
                if (count>0){
                    holder.majorItem.setBackgroundColor(ContextCompat.getColor(context,R.color.pink_light));
                }else{
                    holder.majorItem.setBackgroundColor(ContextCompat.getColor(context,R.color.white));

                }
            }
            if(defItem==position){
                holder.majorItem.setChecked(true);
            }else {
                holder.majorItem.setChecked(false);
            }
        }else{
            holder.majorItem.setText("没数据");
        }
        holder.majorItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnClick.click(position);
            }
        });
        return convertView;
    }

    class ViewHolder{
        RadioButton majorItem;
    }

    public interface BtnClick{
        void click(int position);
    }
}
